package com.jt.assistant.custom_view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.jt.assistant.R
import com.jt.assistant.adapter.DialogListAdapter

class ItemEdit : FrameLayout {

    constructor(context: Context) : super(context) {
        initView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context, attrs)
    }

    lateinit var mEdit: EditText
    lateinit var mTvTitle: TextView

    @SuppressLint("RtlHardcoded", "UseCompatLoadingForDrawables")
    private fun initView(context: Context, attrs: AttributeSet?) {
        val view: View = LayoutInflater.from(context).inflate(R.layout.item_edit, this, true)
        mEdit = view.findViewById<EditText>(R.id.tv_edit)
        mTvTitle = view.findViewById<TextView>(R.id.tv_title)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemEdit)
        val type = typedArray.getString(R.styleable.ItemEdit_type)
        val hint = typedArray.getString(R.styleable.ItemEdit_ItemEditHint)
        val focusable = typedArray.getBoolean(R.styleable.ItemEdit_ItemEditFocusable, true)
        val choose = typedArray.getBoolean(R.styleable.ItemEdit_ItemEditChoose, false)


        if (type != null) mTvTitle.text = type
        if (hint != null) mEdit.hint = hint
        mEdit.isFocusable = focusable//跳转式Item
        if (choose) {//选择式Item
            mEdit.isFocusable = false
            mEdit.setCompoundDrawablesWithIntrinsicBounds(
                null,
                null,
                AppCompatResources.getDrawable(context, R.mipmap.three),
                null
            )
        }
        typedArray.recycle()
    }


    fun setEditText(text: String) {
        mEdit.setText(text)
    }

    fun setTypeText(text: String) {
        mTvTitle.text = text
    }

    fun setCurrentPosition(position: Int) {
        mEdit.setText(mData[position])
        mCurrentPosition = position
    }

    var mData = ArrayList<String>()
    var mCurrentPosition = 0


    fun showListDialog(title: String, context: AppCompatActivity) {
        val dialog = MaterialDialog(context).show {
            title(text = title)
            customView(R.layout.layout_dialog_list, scrollable = true, horizontalPadding = false)
            lifecycleOwner(context)
        }
        // Setup custom view content
        val customView = dialog.getCustomView()
        val rvDialog: RecyclerView = customView.findViewById(R.id.rv_dialog)
        val listAdapter = DialogListAdapter(ArrayList<String>())
        with(rvDialog) {
            layoutManager = LinearLayoutManager(context)
            adapter = listAdapter
            listAdapter.data.addAll(mData)
            listAdapter.notifyDataSetChanged()
        }
        listAdapter.setOnItemClickListener { adapter, view, position ->
            mCurrentPosition = position
            mEdit.setText(mData[position])
            dialog.dismiss()
            if (mClickListener != null) mClickListener!!.onItemClick(position)
        }
    }

    private var mClickListener: OnClickLister? = null

    fun setOnItemClickListener(listener: OnClickLister) {
        this.mClickListener = listener
    }

    interface OnClickLister {
        fun onItemClick(position: Int)
    }


}


